#import "uModules.h"

@implementation uModules

// 导出模块，不添加参数即默认为这个类名
RCT_EXPORT_MODULE();

#pragma mark - Response
//// 导出方法，桥接到js的方法返回值类型必须是void
///* 回调参数必须为两个，第一个为状态，第二个为参数 */
//RCT_EXPORT_METHOD(getBuildType:(RCTResponseSenderBlock)callback){
//  NSInteger callbackData = 1; //准备回调回去的数据
//#ifdef DEBUG
//  callbackData = 0;
//#endif
//#ifdef STAGING
//  callbackData = 2;
//#endif
//  callback(@[@(callbackData)]);
//  return;
////  callback(@[[NSNull null],callbackData]);
//}
// 获取 Debug/Release
  // 区分 Debug/Release/Staging为三个App 
  // 不然每次真机测试都被覆盖安装
RCT_EXPORT_METHOD(getBuildType: (RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject)
{
  NSInteger type = 1;
#ifdef DEBUG
  type = 0;
#endif
#ifdef STAGING
  type = 2;
#endif
  @try{
    resolve(@(type));
  } @catch (NSException *exception) {
    NSError *error = [NSError errorWithDomain:@"error" code:1 userInfo: exception.userInfo];
    reject(exception.name, exception.reason, error);
  }
}

// RCT_EXPORT_METHOD(isDebug:(RCTResponseSenderBlock)callback){
// #ifdef DEBUG
//   callback(@[@(YES)]);
//   return;
// #endif
//   callback(@[@(NO)]);
// }



//回调
//RCTResponseSenderBlock
//RCTResponseSenderBlock只接受一个参数,为数组，把需要回调的参数加入到数组中，回调回去
//RCT_EXPORT_METHOD(getVersionName:(RCTResponseSenderBlock)callback){
//  // 获取info字典
//  NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
//  NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
//  NSString *version = [infoDict objectForKey:@"CFBundleShortVersionString"];
//
//  //更多参数放到数组中进行回调
//  // callback(@[string,array,end]);
//  callback(@[version]);
//  return;
//  callback(@[[NSNull null],version]);
//  // 或者
//  NSDictionary *tempInfoDict = [[NSBundle mainBundle] infoDictionary];
//  NSString *tempExecutable = [tempInfoDict objectForKey:@"CFBundleExecutable"];
//  NSString *tempAppName = [tempInfoDict objectForKey:@"CFBundleDisplayName"];
//  NSString *tempVersion = [tempInfoDict objectForKey:@"CFBundleShortVersionString"];
//  NSLog(@"%@",infoDict);
//}

// 获取版本(已写好脚本, Build之后, 会自动从package.json获取版本写入到info.plist)
RCT_EXPORT_METHOD(getVersionName:(RCTPromiseResolveBlock)resolve:(RCTPromiseRejectBlock)reject)
{
  // 获取info字典
  NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
  @try{
    resolve(@[version]);
  } @catch (NSException *exception) {
    NSError *error = [NSError errorWithDomain:@"error" code:1 userInfo: exception.userInfo];
    reject(exception.name, exception.reason, error);
  }
}
  
// 获取热更新key
RCT_EXPORT_METHOD(getCodePushKey:(RCTPromiseResolveBlock)resolve:(RCTPromiseRejectBlock)reject)
{
  // 获取info字典
  NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CodePushDeploymentKey"];
  @try{
    resolve(@[version]);
  } @catch (NSException *exception) {
    NSError *error = [NSError errorWithDomain:@"error" code:1 userInfo: exception.userInfo];
    reject(exception.name, exception.reason, error);
  }
}

@end

